﻿using Autofac;
using Autofac.Extras.DynamicProxy;
using SqlSugar;
using SunnyPaine.Autofac.Extension.SqlSugar.Entities;
using SunnyPaine.Autofac.Extension.SqlSugar.Interceptor;
using SunnyPaine.Autofac.Extension.SqlSugar.IOC;
using SunnyPaine.Autofac.Extension.SqlSugar.Repository;
using SunnyPaine.Autofac.Extension.SqlSugar.Service;
using SunnyPaine.Autofac.Extension.SqlSugar.Test.Dao;
using SunnyPaine.Autofac.Extension.SqlSugar.Test.Dao.Impl;
using SunnyPaine.Autofac.Extension.SqlSugar.Test.Model;
using SunnyPaine.Autofac.Extension.SqlSugar.Test.Service;
using SunnyPaine.Autofac.Extension.SqlSugar.Test.Service.Impl;
using SunnyPaine.Autofac.Extension.Stereotype;
using SunnyPaine.Autofac.Extension.Stereotype.Attributes;
using SunnyPaine.SqlSugar.Autofac.Stereotype.Selector;
using System;
using System.Collections.Generic;

namespace SunnyPaine.Autofac.Extension.SqlSugar.Test
{
    class Program
    {
        static IContainer Container;


        static void Main(string[] args)
        {
            ////手动注册
            //try
            //{
            //    ContainerBuilder builder = new ContainerBuilder();
            //    SqlSugarScope sqlSugarScope = SqlSugarManager.CreateDBA(GetConnectionInfos());
            //    builder.RegisterType<TransactionInterceptor>().AsSelf().OnActivating(p => p.Instance.Init(sqlSugarScope, infos));
            //    builder.Register(c => sqlSugarScope).As<SqlSugarScope>();
            //    builder.RegisterType<UserDao>().As<IRepository<User>>().As<IUserDao>();
            //    builder.RegisterType<GoodsDao>().As<IRepository<Goods>>().As<IGoodsDao>();
            //    builder.RegisterType<UserService>().As<IService<User>>().As<IUserService>()
            //           .EnableInterfaceInterceptors()uai
            //           .InterceptedBy(typeof(TransactionInterceptor));

            //    Container = builder.Build();
            //    IUserService userService = Container.Resolve<IUserService>();
            //    User user = new User()
            //    {
            //        Id = "u" + DateTime.Now.ToString("yyyyMMddHHmmssfff"),
            //        Name = "admin",
            //        Password = "123456"
            //    };
            //    userService.Add(user);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.Message);
            //}



            //自动注册
            try
            {
                ContainerBuilder builder = new ContainerBuilder();//创建Autofac容器
                builder.UseAutoRegister();//启用自动注册实例
                builder.UseSqlSugar(connectionInfo: GetConnectionInfos());//启用SqlSugar扩展
                builder.RegisterEx();//注册实例，重写了Injecter.Register(ContainerBuilder)。请勿与 Injecter.Register(ContainerBuilder) 混用，否则可能导致实例污染

                Container = builder.Build();
                Container.BuildAfter(new BuildAfter());//使用其它扩展组件时，加上这句代码，Container对象才能正常被使用


                //使用Autofac的IOC

                ////使用了自动事务
                ////IUserService userService = Container.Resolve<IUserService>();
                //IUserService userService = Container.ResolveNamed<IUserService>("UserService");
                //User user = new User()
                //{
                //    Id = "u" + DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                //    Name = "admin",
                //    Password = "123456"
                //};
                //userService.Add(user);

                ////没有使用自动事务
                //IGoodsService goodsService = Container.Resolve<IGoodsService>();
                //Goods goods = new Goods()
                //{
                //    Id = "g" + DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                //    Name = $"水杯",
                //    Price = new decimal(98.99D)
                //};
                //goodsService.Add(goods);

                //User tmp= userService.FindById<string>(user.Id);


                IProjectService projectService = Container.ResolveNamed<IProjectService>("ProjectService");
                List<Project> projects = projectService.FindAll();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }


        static List<ConnectionInfo> GetConnectionInfos()
        {
            List<ConnectionInfo> infos = new List<ConnectionInfo>()
            {
                new ConnectionInfo()
                {
                    ConfigId = "test1",
                    ConnectionString = "Data Source=test1.db3;",
                    DbType = DbType.Sqlite,
                    InitKeyType = InitKeyType.Attribute,
                    IsAutoCloseConnection = true
                },
                //new ConnectionInfo()
                //{
                //    ConfigId = "mysql",
                //    ConnectionString = "server=localhost;Database=test;Uid=root;Pwd=root1234;",
                //    DbType = DbType.MySql,
                //    InitKeyType = InitKeyType.Attribute,
                //    IsAutoCloseConnection = true,
                //    DisableNvarchar = true,
                //    TableEnumIsString = true,
                //}
            };
            return infos;
        }
    }
}
